package com.sdvdxl.util;
import java.io.IOException;   
  
import javax.servlet.Filter;   
import javax.servlet.FilterChain;   
import javax.servlet.FilterConfig;   
import javax.servlet.ServletException;   
import javax.servlet.ServletRequest;   
import javax.servlet.ServletResponse;   
/**  
 * 字符编码Filter  
 * @author www.jokry.com  
 *  
 */  
public class EncodingFilter implements Filter{   
  
    private String targetEncoding = "UTF-8";   
    private FilterConfig filterConfig = null;   
  
    public void init(FilterConfig filterConfig) throws ServletException {   
        //获取配置文件   
        this.filterConfig = filterConfig;   
        //获取配置文件中的编码信息   
        this.targetEncoding = this.filterConfig.getInitParameter("encoding");   
    }   
  
    public void doFilter(ServletRequest request, ServletResponse response,   
            FilterChain filterChain) throws IOException, ServletException {   
        request.setCharacterEncoding(this.targetEncoding);   
        response.setCharacterEncoding(this.targetEncoding);
        filterChain.doFilter(request, response);   
    }   
  
    public void destroy() {   
        this.filterConfig = null ;   
        this.targetEncoding = null ;   
    }   
}  